<!--头部公共部分-->
{include file="common/header" /}

<div id="wodekehu" class="x-body" class="clear:both;">
  <div class="layui-form layui-col-md12 x-so layui-form-pane" >
      <button class="layui-btn layui-btn-primary" id="return_course_lists">
          <i class="layui-icon layui-icon-prev"></i>返回课程管理</button>
            <div class="layui-input-inline" style="float: right;margin-right: 20px;">
                <button class="layui-btn layui-btn-primary" id="edit_course">
                    <i class="layui-icon layui-icon-file-b"></i>编辑课程</button>
                <button class="layui-btn layui-btn-primary" id="delete_course">
                    <i class="layui-icon"></i>删除课程</button>
            </div>
    </div>

 <fieldset class="layui-elem-field" style="border-width: 2px; height: 180px;  line-height: 50px; ">
     <span style="color: #00b7ee;font-size: 18px;  margin-left: 10px; ">{$course_infos.course_name}</span>
     <hr style="margin-right: 10px; margin-left: 10px;">
     <div class="layui-row">
         <div class="layui-col-md3"  style="margin-left: 10px;">
             <div style="line-height:22px;">
                 <span style="font-size: 14px;">科目：{$course_infos.course_name}</span><br/>
                 <span style="font-size: 14px;">年级：{$course_infos.student_grade}</span><br/>
                 <span style="font-size: 14px;">类型：{$course_infos.course_type}</span><br/>
                 <span style="font-size: 14px; color: #f7333d">状态：{$course_infos.status}</span>
             </div>
         </div>
         <div class="layui-col-md3" style="margin-left: 10px;">
             <div style="line-height:22px">
                 <span style="font-size: 14px;">课程单价：{$course_infos.price}元</span><br/>
                 <span style="font-size: 14px;">单位：{$course_infos.unit}</span><br/>
                 <span style="font-size: 14px;">课程次数：{$course_infos.class_num}次/{$course_infos.unit}</span><br/>
                 <span style="font-size: 14px;">单次时长：{$course_infos.class_time}/分钟</span>
             </div>
         </div>
         <div class="layui-col-md3" style="margin-left: 10px;">
             <div style="line-height:22px">
                 <span style="font-size: 14px;">描述：{$course_infos.describe}</span><br/>
             </div>
         </div>
     </div>
 </fieldset>
 <div class="layui-tab layui-tab-card" lay-filter="filter">
     <ul class="layui-tab-title">
                 <li class="layui-this">班级列表</li>
                 <li>课程目录</li>
      </ul>

     <div class="layui-tab-content" >
         <div class="layui-tab-item layui-show">
                <div class="layui-row"
                     style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
                    <div class="layui-form layui-col-md12 x-so layui-form-pane"  lay-filter="form-organization">
                        <div class="layui-input-inline">
                            <select name="campus_id" disabled>
                                <option value="">选择校区</option>
                                {volist name="campus" id="campu"}
                                <option value="{$campu.id}" selected>{$campu.campus_name}</option>
                                {/volist}
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <!--
                            <input type="text" name="grades_name" placeholder="请输入班级名称" autocomplete="off" class="layui-input">
                            -->
                            <select name="grades_name" lay-search="">
                                <option value="">请输入班级名称</option>
                                {volist name="grade" id="vo"}
                                <option value="{$vo.grades_name}">{$vo.grades_name}</option>
                                {/volist}
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" style="width: auto;" placeholder="创建时间" name="grades_date" id="start">
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn" id="search">
                                <i class="layui-icon layui-icon-search"></i>筛选</button>
                            <button class="layui-btn layui-btn-warm" id="clear">
                                <i class="layui-icon"></i>重置</button>
                            <button class="layui-btn layui-btn-normal" id="table-add">
                                <i class="layui-icon layui-icon-add-circle-fine"></i>新增班级</button>
                            <button class="layui-btn layui-btn-danger" id="end-course">结业课程</button>
                        </div>
                    </div>
                </div>
                <input type="hidden"  id="room_id">
                <input type="hidden"  id="room_name">
                <script type="text/html" id="grades_name">
                    <a lay-event="grades_infos" class="layui-table-link">{{ d.grades_name }}</a>
                </script>
                <table class="layui-hide" id="LAY_table_user" lay-filter="table"></table>
                <script type="text/html" id="barTable">
                    {{# if(d.status == 1  && d.class_status == 1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" ><span title="补课班级请前往【排课管理】中添加临时学员">分班<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                    {{# } else if(d.status == 1  && d.class_status == 0){ }}
                    <a class="layui-btn layui-btn-xs" lay-event="class_assignment"><span title="已分配学员">分班<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                    {{# } else{ }}
                    <a class="layui-btn layui-btn-xs" lay-event="class_assignment">分班</a>
                    {{# } }}


                    {{# if(d.status == 1){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" ><span title="已开班的班级无法修改排课信息">修改排课<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                    {{# } else { }}
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="change_paike">修改排课</a>
                    {{# } }}


                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="head_teacher">修改班主任</a>


                    {{# if(d.status == 0){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-disabled" ><span title="已分班才可使用选位功能">排座<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
                    {{# } else { }}
                    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="choice_seat">排座</a>
                    {{# } }}

                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">结业</a>

                </script>
         </div>
         <div class="layui-tab-item ">
             <div id="schedule" class="x-body" class="clear:both;">
                 <table class="layui-table" id="datalist" lay-filter="Event"></table>
             </div>
         </div>
     </div>
</div>
</div>




<!--底部公共部分-->
{include file="common/footer" /}

<script>

    //班主任
    function setClass_id(id,room_name) {
        $("#room_id").val(id);
        $("#room_name").val(room_name);

    }


    layui.use(['form', 'table','laydate',  'element', 'layer', 'laytpl'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            element = layui.element,
            laydate = layui.laydate

        var loading = layer.load();
        var grades_course_id = '{$course_id}';
        var campus_id = '{$campus_id}';
        $('#table-add').click(function () {

            var url = '{:url("Grades/add")}?course_id='+grades_course_id + '&campus_id=' + campus_id+'&add_status='+ 1;
            location.href = url;
        });
        //数据表格 方法级渲染
        var tableIns_grades = table.render({
            elem: '#LAY_table_user'
            , url: '{:url("Grades/grades_course")}?course_id='+ grades_course_id + '&campus_id=' + campus_id
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'id', title: '班级ID', width: 100, sort: true, fixed: false, align: 'center'
                }, {field: 'course_id', title: '课程ID', width: 300, align: 'center'
                }, {field: 'grades_name', title: '班级名称',style:'cursor: pointer; color:#01AAED;font-weight: bold;', width: 400, align: 'center', templet: '#grades_name'
                }, {field: 'campus_name', title: '归属校区', width: 100, align: 'center'
                }, {field: 'room_id', title: '教室ID', width: 300, align: 'center'
                }, {field: 'course_name', title: '课程名称', width: 300, align: 'center'
                }, {field: 'teacher', title: '上课老师', width: 100, align: 'center'
                }, {field: 'headmaster', title: '班主任', width: 100, align: 'center'
                }, {field: 'assistant', title: '助教', width: 100, align: 'center'
                }, {field: 'class_start_date', title: '开班日期', width: 120, align: 'center'
                }, {field: 'class_end_date', title: '结业日期', width: 120, align: 'center'
                }, {field: 'holiday_status', title: '是否跳过节假日?', width: 150, align: 'center'
                }, {field: 'repeat_status', title: '重复类型(周/日)', width: 200, align: 'center'
                }, {field: 'project_num', title: '计划课次(次)', width: 150, align: 'center'
                }, {field: 'class_time_place', title: '上课时间', width: 400, align: 'center'
                }, {field: 'preoccupation', title: '预招人数(人)', width: 150, align: 'center'
                }, {field: 'already_class', title: '已排课次(次)', width: 150, align: 'center'
                }, {field: 'already_arranged', title: '上课进度(次)', width: 150, align: 'center'
                }, {field: 'remarks', title: '备注', width: 100, align: 'center'
                }, {field: 'status_text', title: '是否开班?', width: 100, align: 'center'
                }, {field: 'create_time', title: '创建时间', width: 200, align: 'center'
                }, {fixed: 'right', title: '操作', width: 350, align: 'center', toolbar: '#barTable'}
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
          //      $("[data-field='status_text']").css('display','none');
                $("[data-field='course_id']").css('display','none');
                $("[data-field='room_id']").css('display','none');
                layer.close(loading);
            }
        });
        //日期
        laydate.render({
            elem: '#start',
            range: true
            ,trigger: 'click'
        });
        table.on('edit(table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            if(field === 'headmaster'){
                $.post('{:url("Grades/edit")}'
                    ,{
                        'id':data.id,
                        'headmaster':value
                    }
                    ,function(result){
                        if(result.code === 1){
                            location.reload();
                        }
                  });
            }
        });
        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') { //删除
                layer.confirm('结业将会删除相关班课记录,确定结业吗?', function (index) {
                    $.post("{:url('Grades/del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                            setTimeout(function () {
                                obj.del(); //删除对应行（tr）的DOM结构
                                location.reload();
                            }, 2000)
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });
            } else if (layEvent === 'paike') {
                location.href = '{:url("Grades/arranging")}?id=' + data.id;
            }else if (layEvent === 'class_assignment') {
                layer.open({
                    type: 2,
                    closeBtn: 2,
                    title: '分班',
                    area: ['95%', '95%'],
                    shadeClose: false,
                    content: "{:url('Grades/class_assignment')}?course_id="+data.course_id  + "&room_id=" + data.room_id + "&course_name=" + data.course_name + "&id=" + data.id  + '&campus_id=' + campus_id,

                });

            }else if (layEvent === 'change_paike') {  //修改排课
                layer.open({
                    type: 2,
                    closeBtn: 2,
                    title: '',
					area: ['90%', '98%'],
                    shadeClose: false,
                    content: '{:url("Grades/change_grades")}?id='+data.id  + '&campus_id=' + campus_id+'&status=' + 1,
                    end:function () {

                        tableIns_grades.reload();
                    }
                });
            }else if (layEvent === 'head_teacher') {
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    closeBtn: 2,
                    title: '选择班主任',
                    // btn: ['确定', '取消'],
                    area: ['90%', '90%'],
                    shadeClose: false,
                    content: "{:url('Grades/changeheadmaster')}?id=" + data.id  + '&campus_id=' + campus_id
                });
            } else if (layEvent === 'check') {
                var status;
                if (data.status === 0) {
                    status = 1;
                } else {
                    status = 0;
                }
                //激活与禁用状态改变
                $.post("{:url('Grades/classmanage_status')}", {
                    id: data.id,
                    status: status
                }, function (rJson) {
                    if (rJson.code === 1) {
                        layer.msg(rJson.msg, {
                            time: 2000
                        });
                        setTimeout(function () {
                            location.reload();
                        }, 2000)
                    } else {
                        layer.msg(rJson.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }, "json")
            }else if (layEvent === 'choice_seat') {

                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    closeBtn: 2,
                    title: '选择教室选座',
                    // btn: ['确定', '取消'],
                    area: ['30%', '40%'],
                    shadeClose: false,
                    content: "{:url('Grades/choice_room')}?id=" + data.id,
                    end:function () {
                        var room_id = $("#room_id").val();
                        var room_name = $("#room_name").val();
                        if(room_id == '' || room_id.length <= 0 || room_name == '' || room_name <= 0){
                            return;
                        }
                        var url = '{:url("Classroom/seat_selection")}?id='+room_id + '&class_subject_id='+data.id  + '&campus_id=' + campus_id;
                        layer.open({
                            type: 2,
                            skin: 'layui-layer-lan',
                            closeBtn: 2,
                            title: '【'+room_name+'】'+'---'+'选座',
                            // btn: ['确定', '取消'],
                            area: ['90%', '99%'],
                            shadeClose: false,
                            content: url,
                            cancel: function(index, layero){
                                location.reload();

                                return false;
                            }
                        });

                    }
                });

            }else if(layEvent === 'grades_infos'){   //点击班级名称
                //弹出新的界面
                location.href = "{:url('Arranging/arrang_course')}?grades_id="+data.id  + '&campus_id=' + campus_id;

            }
        });
        //搜索项
        $('#search').click(function () {
            var field = {};
            // field.status = $('select[name=status] option:selected').val();
            field.grades_name = $('select[name=grades_name]').val();
            var create_date_rang = $('input[name=grades_date]').val();
            field.campus_id = $('select[name=campus_id] option:selected').val();
            if(create_date_rang.length > 0){
                strs_date=create_date_rang.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }

            console.log(field);
            tableIns_grades.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });//清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns_grades.reload({
                where: {
                    'grades_name': '',
                    'grades_date':'',
                    'campus_id':'',
                    // 'status': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //结业课程
        $('#end-course').click(function () {
            layer.open({
                skin: 'layui-layer-lan',
                type: 2,
                closeBtn: 2,
                title: '已结业课程',
                area: ['98%', '95%'],
                shadeClose: false,
                content: "{:url('grades/end_grades_course')}?course_id="+grades_course_id + '&campus_id=' + campus_id
            });
        });
        //编辑课程
        $('#edit_course').click(function () {
            location.href = "{:url('course/edit')}?id=" + grades_course_id + '&campus_id=' + campus_id
        });
        //删除课程
        $('#delete_course').click(function () {
            let url = "{:url('Course/del')}";
            let data ={"ids":grades_course_id};
            layer.confirm('确认是否删除该课程?', function () {
                $.post(url,data,function(response){
                    layer.msg(response.msg);
                    if(response.code === 1){
                        location.href = "{:url('course/lists')}";
                    }
                });
            });
        });

        //返回课程
        $('#return_course_lists').click(function () {
            location.href = "{:url('course/lists')}";
        });
        /***************课程目录***************/
            //方法级渲染
        let tableIns = table.render({
                elem: '#datalist'
                , url: '{:url("Schedule/lists")}?course_id=' + grades_course_id + '&campus_id=' + campus_id
                , method: 'POST'
                , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
                , loading: true
                , parseData: function (res) { //res 即为原始返回的数据
                    if (res.code === 1) {
                        res.code = 0;
                    } else {
                        res.code = -1;
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.data //解析数据列表
                    };
                },
                cols: [[
                    {type: 'checkbox'},
                    {field: 'class_times',align: 'center', title: '课次'},
                    {field: 'class_chapter',align: 'center',edit: 'text', title: '章节名称'},
                    {field: 'class_content',align: 'center',edit: 'text', title: '上课内容'},
                ]]
                , height: 'full-180'
                , page: true
                , done: function () {
                    layer.close(loading);
                }
            });
        //监听头工具栏事件
        table.on('toolbar(Event)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        title: '新增上课进度',
                        shadeClose: true,
                        shade: 0.5,
                        area: ['70%', '75%'],
                        content: '{:url("Schedule/add")}?course_id=' + grades_course_id + '&campus_id=' + campus_id
                    });
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                        return;
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                        return;
                    } else {
                        let id = checkStatus.data[0].id;
                        layer.open({
                            type: 2,
                            title: '编辑上课进度',
                            shadeClose: true,
                            shade: 0.5,
                            area: ['70%', '75%'],
                            content: '{:url("Schedule/edit")}?id=' + id + '&course_id=' + grades_course_id + '&campus_id=' + campus_id
                        });
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请至少选择一行');
                        return;
                    }
                    let checked_ids = [];
                    let file_id= 0;
                    for (let index = 0; index < data.length; index++) {
                        const element = data[index];
                        checked_ids.push(element.id)
                        file_id = element.file_id;
                    }
                    let ids ={"ids":checked_ids.join(',')};
                    console.log(ids);
                    let url = '{:url("Schedule/del")}';
                    $.post(url,ids,function(response){
                        console.log(response);
                        if(response.code === 1){
                            tableIns.reload();
                        }else{
                            layer.msg(response.msg);
                        }
                    });
                    break;
            };
        });
    });
</script>